package org.restql.db.resource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.restql.db.Utility;
import org.restql.db.WhereClauseGenerator;

public class ReadOnlyDatabaseResourceImpl extends ReadOnlyDatabaseResource {

	List<String> select;
	List<String> from;
	List<String> where;
	String resourceName;
	String dbName;
	DataSource dataSource;
	private String jdbcUrl;
	private String jdbcDriver;
	private String user;
	private String password;

	public void setSelect(List<String> select) {
		this.select = select;
	}
	public void setFrom(List<String> from) {
		this.from = from;
	}
	public void setWhere(List<String> where) {
		this.where = where;
	}
	public String getResourceName() {
		return resourceName;
	}
	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}
	public String getDbName() {
		return dbName;
	}
	public void setDbName(String dbName) {
		this.dbName = dbName;
	}
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public String getSelectString(RequestResource requestResource, String max, String skip) {
		return WhereClauseGenerator.generateSelectString(requestResource, false, select, where, getConnectString(), max, skip);
	}

	public String getConnectString() {
		return jdbcUrl + "||" + getUser() + "||" + getPassword();
	}
	
	public Connection getConnection(RequestResource requestResource) throws SQLException {
    	this.jdbcUrl = requestResource.getJdbcUrl();
    	this.jdbcDriver = requestResource.getJdbcDriver();;
    	this.user = requestResource.getUser();
    	this.password = requestResource.getPassword();
		return dataSource.getConnection();
	}
	
	public String getUpdateString(RequestResource resource, List<String> fieldList) {
		// TODO Auto-generated method stub
		return null;
	}
	public String getInsertString(RequestResource resource, List<String> fieldList, List<String> valuesList) {
		// TODO Auto-generated method stub
		return null;
	}
	public String getDeleteString(RequestResource resource) {
		// TODO Auto-generated method stub
		return null;
	}
	public String getJdbcDriver() {
		return jdbcDriver;
	}

	public void setJdbcDriver(String jdbcDriver) {
		this.jdbcDriver = jdbcDriver;
	}

	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}
}
